home *** CD-ROM | disk | FTP | other *** search
- #include <Types.h>
- #include <Memory.h>
- #include <Quickdraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <MacWindows.h>
- #include <ControlDefinitions.h>
- #include <Dialogs.h>
- #include <OSUtils.h>
- #include <ToolUtils.h>
- #include <Devices.h>
- #include <StandardFile.h>
- #include <Movies.h>
- #include <Sound.h>
-
- extern void createWindow(void);
- extern void drawWindow( WindowPtr whichWindow );
- extern void clickWindow( WindowPtr whichWindow, Point globalWhere );
-
- typedef struct {
- Component victim;
- Fixed dropRate; // 0 -> never drop, fixed1 -> always drop
- } LousyConnectionSharedGlobalsRecord, **LousyConnectionSharedGlobalsHandle;
- extern LousyConnectionSharedGlobalsHandle getLCSGHandle(void);
- Fixed getDropRate(void);
- void setDropRate( Fixed dropRate );
-
- Boolean gDone = false;
-
- enum {
- kAppleMenuID = 128,
- kAppleMenuAbout = 1,
- kFileMenuID = 129,
- kFileMenuQuit = 1
-
- };
-
- static void doMenu( long menuSelection )
- {
- short whichMenu = HiWord(menuSelection);
- short whichMenuItem = LoWord(menuSelection);
-
- switch (whichMenu) {
- case kAppleMenuID:
- switch (whichMenuItem) {
- case kAppleMenuAbout:
- Alert(128, nil);
- break;
-
- default:
- {
- Str255 daName;
- GetMenuItemText(GetMenuHandle(kAppleMenuID), whichMenuItem, daName);
- OpenDeskAcc(daName);
- }
- break;
- }
- break;
-
- case kFileMenuID:
- switch (whichMenuItem) {
- case kFileMenuQuit:
- gDone = true;
- break;
- }
- break;
- }
- }
-
- int main( void )
- {
- GrafPtr wmgrPort;
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- GetWMgrPort( &wmgrPort );
- SetPort( wmgrPort );
- EnterMovies();
-
- SetMenuBar(GetNewMBar(128));
- AppendResMenu(GetMenuHandle(kAppleMenuID), 'DRVR');
- DrawMenuBar();
-
- if( nil == getLCSGHandle() ) {
- SInt16 itemHit;
- StandardAlert( kAlertStopAlert, "\pI can’t find the LousyConnection component. Is it registered?", "\p", nil, &itemHit );
- gDone = true;
- }
- else {
- createWindow();
- }
-
- while (gDone == false) {
- EventRecord theEvent;
- WindowPtr whichWindow;
- short windowPart;
-
- WaitNextEvent(everyEvent, &theEvent, -1, nil);
-
- switch (theEvent.what) {
- case updateEvt:
- whichWindow = (WindowPtr)theEvent.message;
- SetPort(whichWindow);
- BeginUpdate(whichWindow);
- drawWindow(whichWindow);
- EndUpdate(whichWindow);
- break;
-
- case keyDown:
- if (theEvent.modifiers & cmdKey) {
- doMenu(MenuKey(theEvent.message & charCodeMask));
- }
- break;
-
- case mouseDown:
- windowPart = FindWindow(theEvent.where, &whichWindow);
-
- switch (windowPart) {
- case inDrag:
- DragWindow(whichWindow, theEvent.where, &qd.screenBits.bounds);
- break;
-
- case inGoAway:
- if (TrackGoAway(whichWindow, theEvent.where))
- gDone = true;
- break;
-
- case inContent:
- if (whichWindow != FrontWindow())
- {
- SelectWindow(whichWindow);
- }
- else
- {
- clickWindow(whichWindow, theEvent.where);
- }
- break;
-
- case inMenuBar:
- doMenu(MenuSelect(theEvent.where));
- break;
- }
- break;
- }
- }
-
- return 0;
- }
-
- WindowPtr window = nil;
- ControlHandle control = nil;
-
- void createWindow( void )
- {
- Rect windowBounds = { 50, 10, 90, 210 };
- Rect controlBounds = { 10, 10, 30, 190 };
- Fixed value;
-
- window = NewCWindow( nil, &windowBounds, "\pPacket Loss Rate", true, documentProc,
- (WindowPtr)-1, true, 0);
-
- SetPort( window );
- control = NewControl( window, &controlBounds, "\p", true, 0, 0, 0x100,
- kControlSliderProc + kControlSliderLiveFeedback + kControlSliderHasTickMarks, 0 );
-
- value = getDropRate() >> 8;
- SetControlValue( control, value );
- }
-
- void drawWindow( WindowPtr whichWindow )
- {
- if( whichWindow == window )
- DrawControls( window );
- }
-
- void clickWindow( WindowPtr whichWindow, Point globalWhere )
- {
- Point localWhere = globalWhere;
- Fixed value;
-
- GlobalToLocal( &localWhere );
-
- if( whichWindow == window ) {
- ControlHandle whichControl = 0;
- ControlPartCode part = FindControl( localWhere, window, &whichControl );
-
- if( whichControl == control ) {
- TrackControl( control, localWhere, NULL );
-
- value = GetControlValue( control ) << 8;
- setDropRate( value );
- }
- }
- }
-
-
- // returns false if the capture component isn't there.
- LousyConnectionSharedGlobalsHandle getLCSGHandle(void)
- {
- ComponentDescription cd = { 'rtpr', 'gnrc', 'drop', 0, 0 };
- Component c;
-
- c = FindNextComponent( 0, &cd );
- if( c )
- return (LousyConnectionSharedGlobalsHandle) GetComponentRefcon( c );
- else
- return 0;
- }
- Fixed getDropRate(void)
- {
- LousyConnectionSharedGlobalsHandle sg = getLCSGHandle();
- if( sg && *sg )
- return (*sg)->dropRate;
- else
- return 0;
- }
- void setDropRate( Fixed dropRate )
- {
- LousyConnectionSharedGlobalsHandle sg = getLCSGHandle();
- if( sg && *sg )
- (*sg)->dropRate = dropRate;
- else
- SysBeep(1);
- }
-
-